home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Programming / PPCsiod / sources / port.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  4.7 KB  |  172 lines

  1. /* Scheme In One Define.
  2.  
  3. The garbage collector, the name and other parts of this program are
  4.  
  5.  *                     COPYRIGHT (c) 1989 BY                              *
  6.  *      PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  7.  
  8. Conversion  to  full scheme standard, characters, vectors, ports, complex &
  9. rational numbers, and other major enhancments by
  10.  
  11.  *      Scaglione Ermanno, v. Pirinoli 16 IMPERIA P.M. 18100 ITALY        * 
  12.  
  13. Permission  to use, copy, modify, distribute and sell this software and its
  14. documentation  for  any purpose and without fee is hereby granted, provided
  15. that  the  above  copyright  notice appear in all copies and that both that
  16. copyright   notice   and   this  permission  notice  appear  in  supporting
  17. documentation,  and that the name of Paradigm Associates Inc not be used in
  18. advertising or publicity pertaining to distribution of the software without
  19. specific, written prior permission.
  20.  
  21. PARADIGM  DISCLAIMS  ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  22. ALL  IMPLIED  WARRANTIES  OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  23. PARADIGM  BE  LIABLE  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  24. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  25. IN  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  26. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  
  28. */
  29.  
  30. #include <ctype.h>
  31. #include <setjmp.h>
  32. #include <signal.h>
  33. #include <math.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36.  
  37. #include "siod.h"
  38.  
  39.  
  40. LISP portcons(FILE *fp)
  41. {long flag;
  42.  LISP s,f;
  43.  flag=no_interrupt(1);
  44.  NEWCELL(s,tc_port)
  45.  PORTPTR(s) = fp;
  46.  PORTFLAG(s) = 0;
  47.  no_interrupt(flag);
  48.   return(s);}
  49.  
  50. LISP openport(LISP name,LISP how,LISP buf)
  51. {if(NSTRINGP(name))err("open-port",name,ERR_FIRST | ERR_NSTR);
  52.  if(NSTRINGP(how))err("open-port",how,ERR_SECOND | ERR_NSTR);
  53.  if(NINTNUMP(buf))err("open-port",buf,ERR_THIRD | ERR_NINT);
  54.  return(open_port(SNAME(name),SNAME(how),INTNM(buf)));}
  55.  
  56. LISP portp(LISP x)
  57. {if(PORTP(x))return(truth);
  58.  return(NIL);}
  59.  
  60. LISP open_port(char *name,char *how,int buf)
  61. {LISP z;
  62.  long flag; 
  63.  FILE *fp;
  64.  flag = no_interrupt(1);
  65.  if(strlen(how)>3)
  66.    *(how+3)='\0';
  67.  fp = fopen(name,how);
  68.  if (!fp)
  69.     err(name,NIL,ERR_NFIL);
  70.  if(buf>0)
  71.     setvbuf(fp,NULL,_IOFBF,BUFSIZ);
  72.  else if(buf<0)
  73.     setvbuf(fp,NULL,_IOLBF,BUFSIZ);
  74.  else
  75.     setvbuf(fp,NULL,_IONBF,BUFSIZ);
  76.  z = portcons(fp);
  77.  if(strchr(how,'+'))
  78.    PORTFLAG(z)=0;
  79.  else if(strchr(how,'r'))
  80.    PORTFLAG(z)=1;
  81.  else
  82.    PORTFLAG(z)=-1;
  83.  no_interrupt(flag);
  84.  return(z);}
  85.  
  86. LISP input_portp(LISP port)
  87. {if(PORTP(port) && (PORTFLAG(port)>=0))
  88.   return(truth);
  89.  return(NIL);}
  90.  
  91. LISP output_portp(LISP port)
  92. {if(PORTP(port) && (PORTFLAG(port)<=0))
  93.   return(truth);
  94.  return(NIL);}
  95.  
  96. LISP file_exist(LISP x)
  97. {FILE *fp;
  98.  if NSTRINGP(x) err("file-exists?",x,ERR_GEN_ARG|ERR_NSTR);
  99.  fp = fopen(SNAME(x),"r");
  100.  if (!fp)
  101.   return(NIL);
  102.  fclose(fp);
  103.  return(truth);} 
  104.  
  105. LISP lflush(LISP po)
  106. {if(NPORTP(po))
  107.     err("flush-port",po,ERR_GEN_ARG | ERR_NPOR);
  108.  clearerr(PORTPTR(po));
  109.  if(fflush(PORTPTR(po))==EOF)
  110.     err("IO error during flush-port",NIL,ERR_GEN);
  111.  return(NIL);}
  112.  
  113. LISP close_port(LISP port)
  114. {long flag;
  115.  if(NPORTP(port))err("close-port",port,ERR_GEN_ARG | ERR_NPOR);
  116.  if(PORTPTR(port)==NULL) return(truth);
  117.  flag = no_interrupt(1);
  118.  if (fclose(PORTPTR(port)))
  119.     err("could not close a file",NIL,ERR_GEN);
  120.  PORTPTR(port) = NULL;
  121.  no_interrupt(flag);
  122.  return(truth);}
  123.  
  124. LISP vload(char *fname,LISP env)
  125. {LISP form,port;
  126.  FILE *f;
  127.  port = open_port(fname,"r",1);
  128.  f = PORTPTR(port);
  129.  while(1)
  130.    {form = lreadf(f);
  131.     if EQ(form,eof_val) break;
  132.     leval(form,env);}
  133.  close_port(port);
  134.  return(NIL);}
  135.  
  136. LISP load(LISP form,LISP env)
  137. {LISP fname,nenv;
  138.  fname = leval(car(form),env);
  139.  nenv = leval(car(cdr(form)),env);
  140.  if NSTRINGP(fname) err("load",fname,ERR_FIRST | ERR_NSTR);
  141.  if(NULLP(nenv))
  142.    nenv=env;
  143.  else if(EQ(nenv,sym_user_environment))
  144.    nenv=NIL;
  145.  else if(NENVP(nenv)) err("load",nenv,ERR_SECOND | ERR_NENV);
  146.  return(vload(SNAME(fname),nenv));}
  147.  
  148. LISP getfileposition(LISP port)
  149. {LISP pos;
  150.  long po;
  151.  if(NPORTP(port))err("get-file-position",port,ERR_GEN_ARG | ERR_NPOR);
  152.  po = ftell(PORTPTR(port)); 
  153.  if(po == EOF) err("I/O error occurred during get-file-position",NIL,ERR_GEN);
  154.  pos = intcons(po);
  155.  return(pos);}
  156.  
  157. LISP setfileposition(LISP port,LISP byte,LISP origin)
  158. {long po;
  159.  if(NPORTP(port))err("set-file-position!",port,ERR_FIRST | ERR_NPOR);
  160.  if(NINTNUMP(byte))err("set-file-position!",byte,ERR_SECOND | ERR_NINT);
  161.  if(NINTNUMP(origin))err("set-file-position!",origin,ERR_THIRD | ERR_NINT);
  162.  switch(INTNM(origin))
  163.   {case 0: po = SEEK_SET; 
  164.    break;
  165.    case 1: po = SEEK_CUR; 
  166.    break;
  167.    case 2: po = SEEK_END;
  168.    break;} 
  169.  if(fseek(PORTPTR(port),INTNM(byte),po)) 
  170.       err("I/O error occurred during set-file-position!",NIL,ERR_GEN);
  171.  return(truth);}
  172.